package be.kuleuven.cs.mop.app.controllers;


import static junit.framework.Assert.*;

import java.util.HashMap;
import java.util.HashSet;

import org.junit.*;

import be.kuleuven.cs.mop.TestEnvironment;
import be.kuleuven.cs.mop.app.controllers.ResourceController;
import be.kuleuven.cs.mop.domain.exceptions.InvalidCurrentUserException;
import be.kuleuven.cs.mop.domain.exceptions.TaskManagerException;
import be.kuleuven.cs.mop.domain.model.Resource;
import be.kuleuven.cs.mop.domain.model.Task;
import be.kuleuven.cs.mop.domain.model.impl.Field;
import be.kuleuven.cs.mop.domain.model.impl.FieldType;
import be.kuleuven.cs.mop.domain.model.impl.Interval;
import be.kuleuven.cs.mop.domain.model.impl.ResourceTypeImpl;
import be.kuleuven.cs.mop.domain.model.impl.TaskTypeImpl;
import be.kuleuven.cs.mop.domain.model.impl.UserTypeImpl;



public class ResourceControllerTest {
	
	private static TestEnvironment ENV;
	
	private static ResourceController	ctrl;
	private static Resource				resource;
	
	
	
	@Before
	public void setUp() throws Exception {
		ENV = TestEnvironment.newInstance();
		
		ctrl = ENV.controllers.getControllerResources();
		
		resource = ctrl.createResource(ENV.resourceTypes.get(0), "MyTool");
	}
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {}
	
	@After
	public void tearDown() throws Exception {}
	
	@AfterClass
	public static void tearDownAfterClass() throws Exception {}
	
	@Test(expected=TaskManagerException.class)
	public void testCreateResourceInvalidNull()
			throws InvalidCurrentUserException, TaskManagerException {
		ctrl.createResource(null, null);
	}
	
	@Test(expected=TaskManagerException.class)
	public void testCreateResourceInvalidResourceType()
			throws InvalidCurrentUserException, TaskManagerException {
		ctrl.createResource(ENV.invalidResourceType, "To write on with Chalk");
	}
	
	@Test(expected=TaskManagerException.class)
	public void testCreateResourceInvalidEmptyDescription()
			throws InvalidCurrentUserException, TaskManagerException {
		ctrl.createResource(ENV.resourceTypes.get(0), "");
	}
	
	@Test
	public void testCreateResourceValid()
			throws InvalidCurrentUserException, TaskManagerException {
		final Resource r = ctrl.createResource(ENV.resourceTypes.get(1), "room");
		assertNotNull(r);
		assertTrue(ctrl.getResources().contains(r));
	}
	
	@Test(expected=TaskManagerException.class)
	public void testMakeResourceReservationInvalid()
			throws InvalidCurrentUserException, TaskManagerException {
		ctrl.makeResourceReservation(
				ENV.task1,
				resource,
				ENV.datum1,
				(2 * 24 * 60 * 60000));
		ctrl.makeResourceReservation(
				ENV.task1,
				resource,
				ENV.datum1,
				(2 * 24 * 60 * 60000));
	}
	
	@Test
	public void testMakeResourceReservationValid()
			throws InvalidCurrentUserException, TaskManagerException {
		final HashMap<ResourceTypeImpl, Interval> resources =
			new HashMap<ResourceTypeImpl, Interval>();
		resources.put(ENV.resourceTypes.get(0), new Interval(1, 1));
		final TaskTypeImpl type = new TaskTypeImpl(
				ENV.world, "TempTaskType",
				new HashSet<UserTypeImpl>(ENV.world.getUserTypes()),
				new HashMap<UserTypeImpl, Interval>(),
				new HashMap<String, FieldType>(),
				resources);
		
		final Task task = ENV.controllers.getControllerTasks().createTask(
				ENV.schedule1,
				new HashSet<Task>(),
				new HashMap<String, Field>(),
				type);
		ctrl.makeResourceReservation(
				task,
				ENV.resource1,
				ENV.datum1,
				(2 * 24 * 60 * 60000));
	}
	
}